﻿using databaseoper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{
    public partial class Detail : System.Web.UI.Page
    {
        public DataRow DetailInfo = null;

        private DataRow QueryDetailById(string id)
        {
            string sql = "select * from Products where ProductsID = @Id";
            SqlParameter[] parames = new SqlParameter[] {
                new SqlParameter("@Id",id),
            };
            DataSet data = SQLHelper.ExecuteDataset(SQLHelper.connStr, CommandType.Text, sql, parames);
            if (data == null)
            {
                return null;
            }
            return data.Tables[0].Rows[0];

        }

        private void IncreaseViews()
        {
            string sql = "update Products set views = views + 1 where ProductsID = @ProductsID;";
            SqlParameter[] parames = new SqlParameter[] {
                new SqlParameter("@ProductsID",DetailInfo["ProductsID"]),
            };

            SQLHelper.ExecuteNonQuery(SQLHelper.connStr, CommandType.Text, sql, parames);
        }

        protected void BuyProducts(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>layer.alert('请登录扑街仔！',{ icon: 0, title: '提示' })</script>");
                return;
            }
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>layer.alert('联系电话：" + DetailInfo["Contact"] + "')</script>");

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            string DetailId = Request.QueryString["id"];
            if (DetailId == null)
            {
                Response.Redirect("/index.aspx");
                return;
            }
            DetailInfo = QueryDetailById(DetailId);

            if (DetailInfo == null)
            {
                Response.Redirect("/index.aspx");
                return;
            }
            IncreaseViews();
        }
    }
}